package cn.onein.edu.web.util;

import cn.onein.edu.web.vo.PerpageVO;

/**
 * 分页实体类
 * @author Justin
 *
 */
public class PerPageUtil {

	public static PerpageVO getPerPageVO(Integer totalCounts,Integer currPage,Integer perpageSize){
		Integer totalPages = 0;
		Integer begin = 0;
		Integer end = 0;
		PerpageVO vo = new PerpageVO();
		
		vo.setTotalCounts(totalCounts);
		vo.setCurrPage(currPage);
		
		if(totalCounts == 0){
			return null;
		}
		//计算总分页数
		if(totalCounts % perpageSize == 0){
			totalPages = totalCounts / perpageSize;
		}else{
			totalPages = totalCounts / perpageSize + 1;
		}
		vo.setTotalPages(totalPages);
		//设置前一页，后一页，首页，最后一页，根据当前页
		if(currPage > 1){
			vo.setPrePage(currPage - 1);
			vo.setFirstPage(1);
		}
		if(currPage < totalPages){
			vo.setNextPage(currPage + 1);
			vo.setLastPage(totalPages);
		}
		//显示分页列表，最多显示当前页前2页，一次最多显示10页
		if(currPage - 2 > 1){
			begin = currPage - 2;
		}else{
			begin = 1;
		}
		
		if(begin + 9 > totalPages){
			end = totalPages;
		}else{
			end = begin + 9;
		}
		vo.setBegin(begin);
		vo.setEnd(end);
		
		return vo;
	}
}
